#!/usr/bin/env python
# coding:utf-8
'''
DMZNewAvatar统计
'''
import os,sys,datetime,common.db
from logging import *
from common.date_time import *

def clear_old_goods(cur,day):
    f = open(sys.path[0]+"/config/newgoods.txt")
    s = "("
    for line in f.readlines():
        s += line[:-1] + ","
    s = s[:-1]
    s += ")"
    sql = "delete from statisticdb.DMZNewGoodsTimeDay where date=%s and goodsid not in %s" %(day,s)
    cur.execute(sql)

if __name__ != "__main__":
    
    # 检验输入参数,如果参数为空,返回昨天日期
    if len(sys.argv) > 2:
        strCurDay = sys.argv[2]
        if len(strCurDay) != 10:
            log(ERROR,'Param is error!')
            sys.exit(0)
    else:
        strCurDay = GetDay(-1) ##"2009-03-15"
    log(INFO,'StatDay : '+ strCurDay)
    
    strDay = strCurDay.replace('-','')
    
    # 连接数据库
    cnn = common.db.connect('billDB')
    cur = cnn.cursor()

    delete_sql="delete from statisticdb.DMZNewGoodsTimeDay where date='%s'"%(strDay)
    cur.execute(delete_sql)
    cnn.commit()
    
    sql = "insert into statisticdb.DMZNewGoodsTimeDay select * from statisticdb.DMZGoodsTimeDay where date=%s" %strDay 

    try:
        rowcount00 = cur.execute(sql)
        clear_old_goods(cur,strDay)
    except Exception,data:
        log(ERROR,data)
        sys.exit(0)
    
    cnn.commit()
    cur.close()
    cnn.close()
